package com.Hwadee.eduservice.controller.fron;


import com.Hwadee.commonutils.JwtUtils;
import com.Hwadee.commonutils.R;
import com.Hwadee.commonutils.vo.CommentVo;
import com.Hwadee.eduservice.client.UcenterClient;
import com.Hwadee.eduservice.entity.EduComment;
import com.Hwadee.eduservice.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-11
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/comment")
public class FrontCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient client;

    //分页查询课程评论
    @GetMapping("/getCourseComment/{courseId}/{current}/{limit}")
    public R getCourseComment(@PathVariable("courseId") String courseId,
                              @PathVariable("current") long current,
                              @PathVariable("limit") long limit
                              ) {
        Map<String, Object> courseComment = commentService.getCourseComment(current, limit, courseId);
        return R.ok().data(courseComment);
    }

    //添加评论
    @PostMapping("/addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request) {
        //判断是否登录
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)) {
            return R.error().code(20001).message("请先登录!");
        }
        //用户信息的获取，调用ucenter模块，根据id查用户信息
        comment.setMemberId(memberId);

        CommentVo commentVo = client.getInfo(memberId);
        System.out.println(commentVo);

        comment.setNickname(commentVo.getNickname());
        comment.setAvatar(commentVo.getAvatar());
        System.out.println(comment);

        commentService.save(comment);
        return R.ok().message("添加评论成功!");
    }
}

